export default {
  namespaced: true,
  state: {
    items: {},  // 计费项目
    clockers: {},
    fee: 8,
    handler: null
  },

  getters: {
    // 判断是否有计费项目
    isItemsEmpty: state=>state.items.length == 0 ? true : false,
    // 判断计时器对象clockers中是否含有元素
    isClockersEmpty: state=>Object.keys(state.clockers).length == 0 ? true : false,
  },
  
  mutations: {
    update(state, data) {
        for(var key of Object.keys(data)) {
          state.items[key] = data[key]
          state.clockers[key] = 0
        }

        if(!state.handler)
          this.timing()
    },

    timing(state) {
      state.handler = setInterval(()=>{
        if(state.isItemsEmpty) {
          clearInterval(state.handler)
          state.handler = null
          return
        }

        for(var key of Object.keys(state.clockers))
          state.clockers[key] ++
      }, 1000)
    },

    delete(state, key) {
      delete state.items[key]
      delete state.clockers[key]
    }
  }
}

